/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */

/**
 * @param {ListNode} headA
 * @param {ListNode} headB
 * @return {ListNode}
 */
var getIntersectionNode = function(headA, headB) {

    function getLen(head){
        let cur=head,len=0
        while(cur){
            cur=cur.next
            len++
        }
    return len
    }
    let curA=headA,curB=headB
    let lenA=getLen(headA),lenB=getLen(headB)
    if(lenA<lenB){
        [curA,curB]=[curB,curA];
        [lenA,lenB]=[lenB,lenA]
    }
    let i=lenA-lenB
    while(i--)curA=curA.next
    
    while(curA!==curB&&curA){
        curA=curA.next
        curB=curB.next
    }
    return curA
    
    }